import { groupExpTime, deviceTypeIcon } from '@/utils/index.js'
import { appletDeviceList, AsMsgQ, shareDeviceList } from '@/API/api.js'
export default function(){
 // 获取设备数据
const getAppDeviceList=(callback)=>{
	let header = {
    Authorization:uni.getStorageSync('accessToken'),
    appKey:getApp().globalData.appConfig.xm_appKey
  }
  appletDeviceList(header).then(res=>{
		let data=res.data||[]
		let devList = [];
		if(data&&data.length){
			data.forEach(z=>{
				devList.push({devId:z.deviceNo,devName:z.deviceName,devType:z.pid,devState:0})
			})
			
			callback(getDeviceFormat(devList))
		}
		// uni.setStorageSync('devListVal',devList)//设备数据是否有值
	});
}
// 获取分享得设备数据;
const getShareDeviceList=(callback) => {
	let header = {
		Authorization:uni.getStorageSync('accessToken'),
		appKey:getApp().globalData.appConfig.xm_appKey
	}
	let params = {
		page:1,
		limit:999,
	}
	shareDeviceList(header,params).then(res=>{
		let data=res.data.data||[]
		let devList = [];
		if(data&&data.length){
			data.forEach(z=>{
				devList.push({devId:z.deviceNo,devName:z.deviceName,devType:z.pid,devState:0})
			})

			callback(getDeviceFormat(devList))
		}
		// uni.setStorageSync('devListVal',devList)//设备数据是否有值
	});
}
// 获取设备数据返回处理后新数据
const  getDeviceFormat=async(dataDevices)=>{
		const stateDevice={
			caps: ['xmc.css', 'xmc.voes', 'pms.voes', 'pms.sms', 'net.cellular'],
			devices: [],
		}
		let list = []
		if (dataDevices.length) {
			list = dataDevices.map(t => {
				return new Promise((resolve,reject) => {
					t.icon = deviceTypeIcon(t.devType);
					let data={
						"body": {
							"caps": stateDevice.caps,
							"sn": t.devId
						},
						"method": "reqCaps"
					}
					AsMsgQ(data).then(res => {
						const {ret,caps} = res;
						t.support = false;
						if (ret === 200) {
							let arr = Object.keys(caps);//key值
							stateDevice.caps.forEach(state_caps=>{
								t[state_caps] = {channelsArr:[]};//初始化各商品类型
								arr.forEach(arr_key=>{
									if(arr_key.indexOf(state_caps)>-1){
										t[state_caps][arr_key] = caps[arr_key];
										t[state_caps].sn = t.devId;//设备序列号
										t[state_caps].classifyId = state_caps;//商品标识
										//是否支持
										if(arr_key===`${state_caps}.support` || arr_key===`${state_caps}.vid.support`){
											if(caps[arr_key]){
												t.support = true
											}
										}
										if(arr_key===`${state_caps}.expirationtime` || arr_key===`${state_caps}.vid.expirationtime`){
											if(t[state_caps][arr_key]){
												let isExpireMessage = groupExpTime(t[state_caps][arr_key]*1000);
												t[state_caps].expireTime = isExpireMessage.time;
												t[state_caps].expirationtimeColor = isExpireMessage.color;
											}
										}
										// 多通道到期时间处理
										if(arr_key===`${state_caps}.vid.expirationtime.channels` || arr_key===`${state_caps}.expirationtime.channels`){
											let channelsArr = t[state_caps][arr_key].split('_');//获取各个通道的时间
											t[state_caps].channelsArr = [];//初始化重组数据
											t[state_caps].normalUseChannel = 0;//初始化正常使用的通道数
											let normalUse = 0;//初始化通道正常使用的数量
											// 云存储中多通道图标且设备类型为0改为NVR
											if(state_caps==='xmc.css'){
												if(channelsArr[0]&&t.devType==0){
													t.icon = '0x3';
												}
											}
											// 遍历
											channelsArr.forEach((CHANNEL_TIME,CHANNEL_i)=>{
												if(CHANNEL_TIME){
													if(CHANNEL_TIME&&CHANNEL_TIME!='0'&&CHANNEL_TIME!='-1'){
														let ExpTime = groupExpTime(CHANNEL_TIME*1000);
														t[state_caps].channelsArr.push({chnId:Number(CHANNEL_i),expireTime: ExpTime})//重组数据 chnId：通道号 ，expireTime 时间信息(object)
														// 获取正常使用的通道数
														if(ExpTime.color!='reds'){
															normalUse++
														}
														t[state_caps].normalUseChannel = normalUse;
													}else{
														t[state_caps].channelsArr.push({chnId:Number(CHANNEL_i),expireTime: 0})
													}
												}
											})
										}
									}
								})
							});
							if(t.support){
								stateDevice.devices.push(t)
								// console.log('promise1231231')
								resolve(t)
							}
						}
						resolve()
					})
				})
	  		})
	  	}
		let arr = await Promise.all(list).then((res) => {
			// console.log(res,stateDevice.devices , 'promise')
			let devicesArrary = []
			let devicesArr = res
			res.forEach((item, index) => {
				if(item && Object.keys(item).length) {
					devicesArrary.push(item)
				}
			})
			return devicesArrary
		})
	return arr


}
return{
	getAppDeviceList,
	getShareDeviceList
	}
}